<?php
	/**
	 * Database application utility.
	 * @author IMP
	 */
	
	abstract class DatabaseApplication extends Application {
		private $databaseApplicationInitialized = false, $depo;
		private static $connections = array();
		
		private function initializeDatabaseApplication(){
			$this->depo = new XDataDirectory(PATH_CORE_DATABASES);
			$this->databaseApplicationInitialized = true;
		}
		
		protected function setConnection($name, Database $db){
			if(!$this->databaseApplicationInitialized) $this->initializeDatabaseApplication();
			
			$this->depo->store($name, $db);
		}
		
		protected function getDefaultConnection(){
			return $this->getConnection('default');
		}
		
		protected function getAvailableConnections(){
			if(!$this->databaseApplicationInitialized) $this->initializeDatabaseApplication();
		}
		
		protected function &getConnection($name){
			if(!$this->databaseApplicationInitialized) $this->initializeDatabaseApplication();
			
			if(array_key_exists($name, self::$connections)){
				return self::$connections[$name];
			}elseif($this->depo->containsResource($name)){
				$db = $this->depo->restore($name);
				self::$connections[$name] = $db;
				return self::$connections[$name];
			}else{
				throw new ApplicationException('No database connection "' . $name . '" has been set.');
			}
		}
	}
?>